﻿using Furion;
using OnWork.DataCenter.DAL.DataCenter;
using SqlSugar;
using System.Collections.Concurrent;

namespace OnWork.DataCenter.DAL
{
    /// <summary>
    /// 数据库管理
    /// </summary>
    public class DBManger
    {
        /// <summary>
        /// 数据中心库
        /// </summary>
        public static SqlSugarScope DataCenter
        {
            get
            {
                return GetDbScope("DataCenter");
            }
        }

        /// <summary>
        /// 获取数据库 对象
        /// </summary>
        /// <param name="key">数据库名称</param>
        private static SqlSugarScope GetDbScope(string key)
        {
            //如果字典包含则直接返回
            if (DictSqlSugarScope.ContainsKey(key))
            {
                var sql = DictSqlSugarScope[key];
                if (sql != null)
                {
                    return sql;
                }
            }

            //读取配置
            List<ConnectionConfig> connectionConfigs = App.GetConfig<List<ConnectionConfig>>("ConnectionConfigs");
            var DataConfig = connectionConfigs.Where(x => x.ConnectionString.Contains("DataCenter")).FirstOrDefault();

            if (DataConfig != null)
            {
                return GetSqlSugarScope(DataConfig.DbType, DataConfig.ConnectionString, key);
            }

            //默认使用SqlLite
            var path = AppContext.BaseDirectory.Replace(@"\bin\Debug\net6.0", "");//获取具体路径
            var ConnectionString = @"DataSource=" + path + @"\DataBase\DataCenter.sqlite";
            return GetSqlSugarScope(DbType.Sqlite, ConnectionString, key);
        }

        /// <summary>
        /// 静态字典
        /// </summary>
        private static ConcurrentDictionary<string, SqlSugarScope> DictSqlSugarScope = new ConcurrentDictionary<string, SqlSugarScope>();

        /// <summary>
        /// 获取对象
        /// </summary>
        /// <param name="dbType"></param>
        /// <param name="connStr"></param>
        /// <param name="dictKey">关键数据库Key</param>
        /// <returns></returns>
        private static SqlSugarScope GetSqlSugarScope(DbType dbType, string connStr, string dictKey)
        {
            ////如果字典包含则直接返回
            //if (DictSqlSugarScope.ContainsKey(dictKey))
            //{
            //    var sql = DictSqlSugarScope[dictKey];
            //    if (sql != null)
            //    {
            //        return sql;
            //    }
            //}

            if (!string.IsNullOrEmpty(connStr))
            {
                //创建数据库对象
                SqlSugarScope db = new SqlSugarScope(new ConnectionConfig()
                {
                    ConnectionString = connStr,
                    DbType = dbType,
                    IsAutoCloseConnection = true
                });
                DictSqlSugarScope[dictKey] = db;

                if (connStr.Contains("DataCenter"))
                {
                    DataCenterInit(db); //初始化数据表
                }

                return db;
            }

            return null;
        }

        /// <summary>
        /// 数据库初始化
        /// </summary>
        /// <param name="db">数据库对象</param>
        private static void DataCenterInit(SqlSugarScope db)
        {
            //数据库不存在 则创建
            db.DbMaintenance.CreateDatabase();

            //创建表机构
            Type[] types = typeof(DBConn).Assembly.GetTypes()
            .Where(it => it.IsClass && it.FullName.Contains("OnWork.DataCenter.DAL.DataCenter.") && !it.Name.Contains("DSMqttLog"))//命名空间过滤，当然你也可以写其他条件过滤
            .ToArray();
            db.CodeFirst.SetStringDefaultLength(200).InitTables(types);//根据types创建表
        }
    }
}